#!/usr/bin/env python
# coding:utf-8

"""
Return a dict representing the parsing result

* show_wb
    {
        "cmd":"show_wb",
        "user":None, # default to None
        "group":None # default to None
    }

* send_wb
    {
        "cmd":"send_wb",
        "pic_path": None, # default to None
    }

* send_comment
    {
        "cmd":"send_comment",
        "target_wb": __
    }

* show_following
    {
        "cmd":"show_following",
        "group":None # Default to None
    }

* follow_user
    {
        "cmd": "follow_user",
        "user": __,
        "group": None
    }

* show_groups
    {
        'cmd':'show_groups'
    }

* exception
    {
        "cmd":"exception",
        "error_message": str_showing_the_error
    }
"""


def parse(cmd):
    import re
    # Dispatcher for different function
    top_dispatcher = {
        "send": lambda x: parse_send(x),
        "show": lambda x: parse_show(x),
        "follow": lambda x: parse_follow(x)
    }


    pattern = re.compile("\s")
    components = pattern.split(cmd.lower())
    components = filter(lambda x: x != "", components)
    top_cmd = components[0]
    if top_cmd not in top_dispatcher:
        return { "cmd": "exception", "error_message":"No such command."}
    else:
        return top_dispatcher[top_cmd](components)


def parse_send(args):
    if len(args) < 2:
        return { "cmd": "exception", "error_message":"Syntax error in `send` command"}

    if args[1] == 'wb':
        # send wb                       {'cmd':'send_wb', 'picture':None}
        # send wb picture pic_path      {'cmd':'send_wb', 'picture':pic_path}
        try:
            if len(args) == 2:
                return {'cmd':'send_wb', 'picture':None}
            else:
                return {'cmd':'send_wb', 'picture':args[3]}
        except Exception, e:
            return { "cmd": "exception", "error_message":"Syntax error in `send wb` command"}

    elif args[1] == 'comment':
        # send comment on wb #1         {'cmd':'send_comment', 'weibo':'#1'}
        # send comment on comment #1 of wb #1    {'cmd':'send_comment', 'comment':'#1', 'weibo': #1}
        try:
            if args[3] == 'wb':
                return {'cmd':'send_comment', 'weibo':args[4]}
            elif args[3] == 'comment':
                return {'cmd':'send_comment', 'comment':args[4], 'weibo': args[7]}
            else:
                return { "cmd": "exception", "error_message":"Syntax error in `send comment` command"}

        except Exception, e:
            return { "cmd": "exception", "error_message":"Syntax error in `send comment` command"}
    else:
        return { "cmd": "exception", "error_message":"Syntax error in `send` command"}


def parse_show(args):
    if len(args) < 2:
        return { "cmd": "exception", "error_message":"Syntax error in `show` command"}

    if args[1] == "wb":
        # show wb =>                {'cmd': 'show_wb', 'group': None, 'user': None}
        # show wb from user #1 =>   {'cmd': 'show_wb', 'group': None, 'user': '#1'}
        # show wb from group #1 =>  {'cmd': 'show_wb', 'group': '#1', 'user': None}
        # show wb from exn #1 =>    {'cmd': 'exception', 'error_message': 'Syntax error in `show wb from` command'}
        # show wb from user =>      {'cmd': 'exception', 'error_message': 'Syntax error in `show wb` command'}
        try:
            if "from" in args:
                from_index = args.index("from")
                if args[from_index + 1] == "user":
                    return {"cmd":"show_wb", "user":args[from_index + 2], "group":None}
                elif args[from_index + 1] == "group":
                    return {"cmd":"show_wb", "user":None, "group":args[from_index + 2]}
                else:
                    return {"cmd": "exception", "error_message":"Syntax error in `show wb from` command"}
            else:
                return {"cmd":"show_wb", "user":None, "group":None}
        except Exception, e:
            return { "cmd": "exception", "error_message":"Syntax error in `show wb` command"}


    elif args[1] == "following":
        # show following =>             {'cmd':'show_following', 'group':None}
        # show following groupname      {'cmd':'show_following', 'group':'groupname'}
        try:
            if len(args) == 2:
                return  {'cmd':'show_following', 'group':None}
            else:
                 {'cmd':'show_following', 'group':args[2]}
        except Exception, e:
            return { "cmd": "exception", "error_message":"Syntax error in `show following` command"}

    elif args[1] == 'groups':
        # show groups =>        {'cmd': 'show_groups'}
        # Fuck SINA!
        return {'cmd': 'show_groups'}
    elif args[1] == 'comment':
        try:
            # show comments of wb #1 => {'cmd': 'show_comments', 'weibo':'#1'}
            if len(args) == 5 and args[3] == 'wb':
                return  {'cmd': 'show_comment', 'weibo':args[4]}
            else:
                { "cmd": "exception", "error_message":"Syntax error in `show comment` command"}
        except Exception, e:
            return { "cmd": "exception", "error_message":"Syntax error in `show comment` command"}
    else:
        return { "cmd":"exception", "error_message":"Unable to recognize %s in `show *` command" % args[1]}


def parse_follow(args):
    # follow user username                      {'cmd':'follow_user', 'user':username, 'group':None}
    # follow user usernmae into group groupname {'cmd':'follow_user', 'user':username, 'group':groupname}
    if len(args) < 3:
        return { "cmd": "exception", "error_message":"Syntax error in `follow` command"}

    if args[1] != 'user':
        return { "cmd": "exception", "error_message":"Syntax error in `follow` command"}

    username = args[2]

    if len(args) == 3:
        return {'cmd':'follow_user', 'user':args[2], 'group':None}
    elif len(args) == 6:
        return {'cmd':'follow_user', 'user':args[2], 'group':args[5]}
    else:
        return { "cmd": "exception", "error_message":"Syntax error in `follow` command"}




if __name__ == "__main__":
    print parse('send wb')
    print parse('send wb picture pic.jpg')

